﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class DuyetPhieuDanhGiaNCC : UserControl
    {
        public DuyetPhieuDanhGiaNCC()
        {
            InitializeComponent();
        }

        private void DuyetPhieuDanhGiaNCC_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            dgvPhieuNCC.Rows.Clear();

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var listPhieu = from p in dc.PhieuDanhGiaNCCs
                            where p.deleted == false && p.TrangThai == _DB.TrangThaiPhieu.CHO_DUYET_ID
                            orderby p.NgayTao descending select p;

            int count = 1;
            foreach (_DB.PhieuDanhGiaNCC p in listPhieu)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvPhieuNCC);
                row.Cells[0].Value = count++;
                row.Cells[1].Value = p.MaPhieu;
                row.Cells[2].Value = p.NguoiTao.Ten;
                row.Cells[3].Value = p.NgayTao;
                row.Cells[4].Value = p.HangHoa_NhaCungCaps.Count;

                dgvPhieuNCC.Rows.Add(row);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuNCC.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // update
            string maPhieu = (string) dgvPhieuNCC.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.PhieuDanhGiaNCC p = dc.PhieuDanhGiaNCCs.Single(x => x.MaPhieu == maPhieu);
            p.TrangThai = _DB.TrangThaiPhieu.DA_DUYET_ID;
            dc.SubmitChanges();
            loadData();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuNCC.SelectedRows.Count <= 0)
                return;

            // Input ly do
            OtherForms.InputTextForm tf = new OtherForms.InputTextForm("", "Lý do không duyệt");
            tf.ShowDialog();
            if (tf.OK == false)
                return;
            string lydo = tf.getText();

            // update
            string maPhieu = (string)dgvPhieuNCC.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.PhieuDanhGiaNCC p = dc.PhieuDanhGiaNCCs.Single(x => x.MaPhieu == maPhieu);
            p.TrangThai = _DB.TrangThaiPhieu.KHONG_DUYET_ID;
            p.LyDoKhongDuyet = lydo;
            dc.SubmitChanges();
            loadData();
        }

        private void dgvPhieuNCC_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuNCC.SelectedRows.Count <= 0)
                return;

            string maPhieu = (string)dgvPhieuNCC.SelectedRows[0].Cells[1].Value;
            OtherForms.XemPhieuDanhGiaNCC f = new OtherForms.XemPhieuDanhGiaNCC(maPhieu);
            f.Show();
        }
    }
}
